package com.example.xmltohtml.controller;

import com.example.xmltohtml.service.XmlTransformService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.TransformerException;
import java.io.IOException;

@RestController
@RequestMapping("/api/transform")
@RequiredArgsConstructor
public class XmlTransformController {

    private final XmlTransformService xmlTransformService;

    @PostMapping("/xml-to-html")
    public ResponseEntity<String> transformXmlToHtml(
            @RequestBody String xmlContent,
            @RequestParam(defaultValue = "xslt/fund_profile.xslt") String xsltPath) {
        try {
            String htmlContent = xmlTransformService.transformXmlToHtml(xmlContent, xsltPath);
            return ResponseEntity.ok(htmlContent);
        } catch (TransformerException | IOException e) {
            return ResponseEntity.badRequest().body("Error transforming XML: " + e.getMessage());
        }
    }
} 